package classfile

type MemberInfo struct {
	cp               ConstantPool
	access_flags     uint16
	name_index       uint16
	descriptor_index uint16
	attributes_count uint16
	attributes       []AttributeInfo
}

func (self *MemberInfo) ConstantValueAttribute() *ConstantValueAttribute {
	for _, attrInfo := range self.attributes {
		switch attrInfo.(type) {
		case *ConstantValueAttribute:
			return attrInfo.(*ConstantValueAttribute)
		}
	}
	return nil
}

func (self *MemberInfo) CodeAttribute() *CodeAttribute {
	for _, attrInfo := range self.attributes {
		switch attrInfo.(type) {
		case *CodeAttribute:
			return attrInfo.(*CodeAttribute)
		}
	}
	return nil
}

func (self *MemberInfo) Name() string {
	return self.cp.getUtf8(self.name_index)
}

func (self *MemberInfo) Descriptor() string {
	return self.cp.getUtf8(self.descriptor_index)
}

func (self *MemberInfo) AccessFlags() uint16 {
	return self.access_flags
}

func readMembers(count uint16, reader *ClassReader, cp ConstantPool) []*MemberInfo {
	members := make([]*MemberInfo, count)
	for i := range members {
		members[i] = readMember(reader, cp)
	}
	return members
}

func readMember(reader *ClassReader, cp ConstantPool) *MemberInfo {
	access_flags := reader.readUint16()
	name_index := reader.readUint16()
	descriptor_index := reader.readUint16()
	attributes_count := reader.readUint16()
	return &MemberInfo{
		cp:               cp,
		access_flags:     access_flags,
		name_index:       name_index,
		descriptor_index: descriptor_index,
		attributes_count: attributes_count,
		attributes:       readAttributes(attributes_count, reader, cp),
	}
}
